Client-side Router Cache
https://nextjs.org/docs/app/building-your-application/caching#client-side-router-cache
warning.icon Next 15 から Page コンポーネントでの Client-side Route Cache がデフォルトで無効になった
https://nextjs.org/docs/app/building-your-application/upgrading/version-15#client-side-router-cache
When navigating between pages via <Link> or useRouter, page segments are no longer reused from the client-side router cache.
However, they are still reused during browser backward and forward navigation and for shared layouts.
layout.tsx や loading.tsx では引き続き有効
以前と同じ動作にするには、next.config.js に exprimental.staleTimes を指定する必要がある
https://nextjs.org/docs/app/api-reference/next-config-js/staleTimes
code:next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
staleTimes: {
dynamic: 30,
static: 180,
},
},
}
module.exports = nextConfig
概要
Next.js の 4 種類のキャッシュ の 1 つで、ナビゲーション時のサーバリクエストを減らすことを目的とする
RSC Payload を Router ごとに クライアント側(インメモリ)で 保持する
これによりページナビゲーションの高速化が期待できる
インメモリなので、ページ更新(router.refresh)などでクリアされる
ページアクセスだけでなく、<Link> コンポーネントや router.prefetch によってもキャッシュが生成される
warning.icon Prefetch は本番環境でのみ有効
https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating#2-prefetching
https://nextjs.org/docs/app/api-reference/components/link#prefetch
TTL
prefetch を指定しなかった場合: 30 秒
prefetch={true] や router.prefetch: 5 分間
キャッシュを無効にする方法
1. Server Actions 内で、
On-demand Revalidation を有効にしている
cookies.set や cookies.delete を使用している
2. router.refresh を用いた場合
#Next.js